<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/11
 * Time: 17:44
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Vehicle extends Base {

    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['user_username|user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($province)&&$province!="")
            {
                $map['user_sheng'] = $province;
            }

            if(isset($city)&&$city!="")
            {
                $map['user_shi'] = $city;
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('vehicle')
//                ->join('up_dealer','dealer_id = vehicle_dealer_id','left')
                ->join('up_u_user', 'vehicle_user_id = user_id', 'left')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::name('vehicle')
//                ->join('up_dealer','dealer_id = vehicle_dealer_id','left')
                ->join('up_u_user', 'vehicle_user_id = user_id', 'left')
                ->where($map)
                ->field('vehicle_id, vehicle_status, vehicle_content, vehicle_time, user_username, user_phone')
                ->page($NowPage,$limits)
                ->order('vehicle_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign([
            'province' => $province,
        ]);
        return $this->fetch();
    }

    /**
     * 查看
     */
    public function detail(){
        $vehicle_id = input('param.vehicle_id');
        $res = Db::table('up_vehicle')
            ->join('up_u_user', 'vehicle_user_id = user_id', 'left')
            ->join('up_dealer', 'dealer_id = vehicle_dealer_id', 'LEFT')
            ->where('vehicle_id', $vehicle_id)
            ->field('vehicle_status, vehicle_content, vehicle_time, user_username, user_phone, vehicle_images, 
            vehicle_name, vehicle_code, dealer_name, vehicle_img, vehicle_img_deputy, vehicle_content')
            ->find();
        // 图集
        if(!empty($res['vehicle_images'])){
            $res['vehicle_images'] = explode(',', trim($res['vehicle_images'], ','));
        }else{
            $res['vehicle_images'] = [];
        }
        // 车架号
        $res['vehicle_code'] = trim($res['vehicle_code'], ',');
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    /**
     * 通过
     * @return \think\response\Json
     */
    public function handle(){
        $vehicle_id = input('id');
        $vehicle_info = Db::table('up_vehicle')
            ->where('vehicle_id', $vehicle_id)
            ->field('vehicle_phone, vehicle_user_id')
            ->find();
        $car_url = 'http://cctruck.project.g7s.huoyunren.com/service.php?method=truck.ccappapi.getVehicleTeam';
        $request = $car_url . '&account=c' . $vehicle_info['vehicle_phone'];
        $data = kd_http($request);
        if($data['procStatus'] == 0){
            $team = end($data['teams']);
            $num = count($data['teams']);
            Db::table('up_u_user')->where('user_id', $vehicle_info['vehicle_user_id'])->update([
                'user_update_time' => date('Y-m-d H:i:s'),
                'user_team_id' => $team['teamId'],
                'user_car_num' => $num
            ]);
            $res = Db::table('up_vehicle')->where('vehicle_id', $vehicle_id)->setField('vehicle_status', 2);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }else{
            return json(['code' => 100, 'msg' => $data['message']]);
        }
    }
    
    /**
     * 拒绝
     * @return \think\response\Json
     */
    public function refuse(){
        $vehicle_id = input('id');
        $res = Db::table('up_vehicle')->where('vehicle_id', $vehicle_id)->setField('vehicle_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 备注
     * @return mixed|\think\response\Json
     */
    public function remark(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $vehicle_content = input('vehicle_content');
            $data = [
                'vehicle_content' => $vehicle_content
            ];
            $res = Db::table('up_vehicle')->where('vehicle_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '处理成功']);
            }else{
                return json(['code' => 100, 'msg' => '处理失败']);
            }
        }
        $vehicle_id = input('param.vehicle_id');
        $this->assign('id',$vehicle_id);
        return $this->fetch();
    }
}